<?php

class yun_temporary extends IController
{
    function index()
    {
        echo '';
    }
    //获取app信息
    function get_app_info(){
         $data=array(
             'version'=>'v1.0'
         );
        $this->json_success($data);
    }
    function index_list()
    {
        $data = Api::run('getCategoryListTop');
        $recommend=array();
        foreach ($data as $key=> &$value) {
            $value['goods'] = Api::run('getCategoryExtendList', array('#categroy_id#', $value['id']), 8);
            if (!$value['goods']){
                unset($data[$key]);
            }else{
                $recommend = array_merge($recommend, $value['goods']);
            }
        }
        shuffle($recommend);
        $recommend=array_slice($recommend,0,4);
        $recommend_content=array(
            'id'=>"0",
            'name'=>'商城推荐',
            'parent_id'=>"0",
            'sort'=>"0",
            'visibility'=>'1',
            'keywords'=>"",
            'pic'=>'null',
            'ad_img'=>'null',
            'goods'=>$recommend
        );
        //var_dump($recommend);
        //$data=array_merge($recommend_content,$data);
        array_unshift($data,$recommend_content);
        $banner = Api::run('getBannerList');
        $array = array('banner' => $banner, 'list' => $data);
        $this->json_success($array);
    }

    //[列表页]商品
    function pro_list()
    {
        $this->catId = IFilter::act(IReq::get('cat'), 'int');//分类id

        if ($this->catId == 0) {
            $this->json_error('缺少分类ID', 403);
        }

        //查找分类信息
        $catObj = new IModel('category');
        $this->catRow = $catObj->getObj('id = ' . $this->catId);

        if ($this->catRow == null) {
            $this->json_error('此分类不存在', 403);
        }

        //获取子分类
        $this->childId = goods_class::catChild($this->catId);
        //获取列表
        $data = $this->goods_list(array('category_extend' => $this->childId));
        $this->json_success($data);


    }

    //商家店铺商品列表页
    function home()
    {
        $seller_id = IFilter::act(IReq::get('id'), 'int');
        $sellerRow = Api::run('getSellerInfo', $seller_id);
        if (!$sellerRow) {
            $this->json_error('商户信息不存在');
        }
        $goods_list = $this->goods_list(array('go.seller_id' => $seller_id));
        if(empty(['data'])){
            $goods_list='';
        }else{
            $goods_list= $goods_list;
        }
//        $data = array(
//            'seller_info' => $sellerRow,
//            'data'=>$goods_list
//        );
        $this->json_success($goods_list);
    }

    function goods_list($condition, $pagesize = 5)
    {
        $goodsObj = search_goods::find($condition, $pagesize);
        $resultData = $goodsObj->find();
        $page_info = array(
            'page' => IReq::get('cat') ? IReq::get('cat') : 1,
            'page_total' => $goodsObj->paging->lastpage,
            'page_size' => $goodsObj->paging->pagesize,


        );
        $data = array('data' => $resultData, 'page' => $page_info);
        return $data;
        //$this->json_success($data);
    }

    private function json_success($data = NULL, $msg = '成功', $callback = "")
    {
        yun::ajaxReturn(0, $msg, $data, $callback);
        die();
    }

    private function json_error($msg = '失败', $data = '', $callback = "")
    {
        yun::ajaxReturn(1, $msg, $data, $callback);
        die();
    }
    //用户登录
    function login_act()
    {
        //调用_userInfo登录插件
        $result = plugin::trigger('userLoginAct',$_POST);
        if(is_array($result))
        {
            $model=new IModel('member');
            $u_info=$model->getObj('user_id = "'.$result['id'].'"');
            $token=common::think_encrypt($result['id']);
            $array=array('token'=>$token,'login_info'=>$_POST['login_info'],'password'=>$_POST['password']);

            $array=array_merge($array,$u_info);
            /*
            foreach ( $array as $key => $value ) {
                if ( empty($value) ) {
                    unset($array[$key]);
                }
            }
            //unset($array['prop']);
            $siteConfigObj = new Config("site_config");
            $site_config   = $siteConfigObj->getInfo();
            $url = $site_config['url'].'/index.php?client=amdpord&token='.$token;


            $array=array('token'=>$token,'url'=>$url);
            */
            $this->json_success($array,'登录成功');
        }
        else
        {
            Yun::ajaxReturn(1,$result);
        }
    }
    //用户注册
    function reg_act()
    {
        //调用_userInfo注册插件
        $result = plugin::trigger("mobileUserRegAct",$_POST);
        if(is_array($result))
        {
            $model=new IModel('member');
            $u_info=$model->getObj('user_id = "'.$result['id'].'"');
            $token=common::think_encrypt($result['id']);
            $array=array('token'=>$token,'mobile'=>$_REQUEST['mobile'],'password'=>$_REQUEST['password']);
            $array=array_merge($array,$u_info);
            $this->json_success($array,'注册成功');
        }
        else
        {
            Yun::ajaxReturn(1,$result);
        }
    }
    //app启动时的海报
    function poster(){
        $array=array(
            array('img'=>'http://oaavnhqhs.bkt.clouddn.com/20161019161014717.jpg'),
            array('img'=>'http://oaavnhqhs.bkt.clouddn.com/20161019161109171.jpg'),
            array('img'=>'http://oaavnhqhs.bkt.clouddn.com/20161019161123613.jpg')
        );
        $key=md5(20161020);
        $data=array('images'=>$array,'key'=>$key);
        $count=count($array);
        if($count>0){
            $this->json_success($data,'成功获取海报图片信息');
        }else{
            $this->json_error('无宣传图片');
        }

    }
    function poster_key(){
        $key=md5(20161020);
        $array=array('key'=>$key);
        $this->json_success($array,'成功获取海报key');
    }
}



?>